// pages/details/estimate/estimate.js
var app = getApp();
Page({

  data: {
    illume: 0,//点亮的星星数
    illume_no: 5,//没有点亮的星星数
    noteMaxLen: 200, //详细地址的字数限制 
    currentNoteLen: 0, //输入的字数 
    fillFilePaths:"",
    evaluateContent:"",
    evaluateList:[],
    evaluateURL: app.globalData.bookUrl + '/evaluate/SaveEvaluate.do',
    getEvaluateURL: app.globalData.bookUrl + '/evaluate/selectEvaluate.do',
    getopenidN: app.globalData.bookUrl + '/WXAppletUserInfo/code.do',
  },

  /**生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    var bookId = options.bookId;
    var bookName = options.bookName;
    // var openId = options.openId;
    var borrowFlag = options.borrowFlag;
    that.setData({
      borrowFlag: borrowFlag,
      bookId: bookId,
      bookName: bookName,
      // openId: openId
    });

    wx.login({
      success: function (res) {
        //发起网络请求 
        wx.request({
          url: that.data.getopenidN,
          data: {
            code: res.code
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          method: 'POST',
          //服务端的回掉 
          success: function (result) {
            wx.setStorage({
              key: "openid",
              data: result.data.openid
            })
            that.setData({
              openId: result.data.openid
            });
            that.getEvaluate();
          }

        })
      }
    });



  },
  
  // onUnload: function (e) {
  //   var that = this;
  //   console.log("评价页返回")
  //     wx.switchTab({
  //       url: '../borrow/borrow'
  //     })
  // },
  in_xin: function (e) {
    var in_xin = e.currentTarget.dataset.in;
    console.log(e.currentTarget.dataset.in);
    console.log(e.currentTarget);
    var illume;
    if (in_xin == 'star') {
      illume = Number(e.currentTarget.id)
    } else {
      illume = Number(e.currentTarget.id) + this.data.illume
    }
    this.setData({
      illume: illume,
      illume_no: 5 - illume
    })
  },
  in_feel:function(e){
    var that = this;
    wx.showLoading({
    })
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      extension: ['doc', 'docx', 'txt', 'xls', 'xlsx', 'pdf'],
      success(res) {
        wx.hideLoading();
        var tempFilePaths = res.tempFiles[0].path;
        var tempName = res.tempFiles[0].name;
        that.setData({
          fillFilePaths: tempFilePaths,
          tempName: tempName,
        })
      },
       fail: function (error) {
        // fail
        wx.showToast({
          title: '未选取',
          duration: 1000,
          image: '../../../images/icon/fail.png',
        })
      }
    })
  },
  // bindTextAreaBlur: function (e) {
  //   var that = this;
  //   that.setData({
  //     evaluateContent: e.detail.value
  //   })

  // },    
  submitEvaluate:function(e){
    var that = this;
    if (that.data.illume != "0" || that.data.evaluateContent != "" || that.data.fillFilePaths != ""){
        wx.request({
          url: that.data.evaluateURL,
          data: {
            bookId : that.data.bookId,
            bookName: that.data.bookName,
            openid: that.data.openId,
            bookScore : that.data.illume,
            evaluateContent: that.data.evaluateContent,
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res.data.data)
            if (res.data.code==0){
              wx.showToast({
                title: '评论成功',
                duration: 1000,
                icon: 'success'
              })
              if (that.data.borrowFlag == 1) {
                console.log("评价页返回")
                wx.navigateBack({

                  url: "../borrow/borrow?isBackEstimate=yes",
                })
              } else {
                wx.navigateBack({
                  url: "../loan/loan?isBackEstimate=yes"
                })
              }
            }else{
              wx.showToast({
                title: res.data.msg,
                duration: 1000,
                image: '../../../images/icon/fail.png',
              })
            }
          
          },
          fail: function (error) {
            // fail
            wx.showToast({
              title: '评论失败',
              duration: 1000,
              image: '../../../images/icon/fail.png',
            })
          }
        })
        if (that.data.fillFilePaths != "") {
        wx.uploadFile({
          url: that.data.evaluateURL,// 仅为示例，非真实的接口地址
          filePath: that.data.fillFilePaths,
          name: 'file',
          formData: {
            bookId: that.data.bookId,
            bookName: that.data.bookName,
            openid: that.data.openId,
          },
          success(res) {
            // wx.showToast({
            //   title: "上传成功",
            //   duration: 1000,
            //   icon: 'success'
            // })
            // do something
            
          }
        })
      }

    }else{
   
      // wx.showToast({
      //   title: '请填写评价',
      //   duration: 1000,
      //   image: '../../../images/icon/fail.png',
      // })
      that.powerDrawer();
      setTimeout(function (e) {
        that.setData({
          showModalStatus: false
        })
      }, 1000)
    }
   
    
   
  },
  // onUnload: function () {

  // },
  getEvaluate:function(){
    var that = this;
    wx.request({
      url: that.data.getEvaluateURL,
      data: {
        bookName: that.data.bookName,
        openid: that.data.openId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.showEvaluate(res.data.data); //向前台传递数据
      },
      fail: function (error) {
        // fail
        console.log(error)
      }
    })
  },
  showEvaluate: function (evaluate) {
    var that = this;
    var evaluates = that.data.evaluateList;
    if (evaluate != null) {
      var bookScore = evaluate[0].bookScore;
      var evaluateContent = evaluate[0].evaluateContent;
      that.setData({
        illume: bookScore,
        illume_no: 5 - bookScore,
        evaluateContent: evaluateContent
      });
    } 
  },
  // 字数限制
  input(event) {
    let that = this;
     var evaluateContent = event.detail.value,
  
    len = parseInt(evaluateContent.length);
    that.setData({
      evaluateContent: evaluateContent
    })
    this.setData({ currentNoteLen: len });
  },
  //我要推荐
  powerDrawer: function (e) {
    this.setData({
      showModalStatus: true
    });
  },

 

})